package ijij41.togetherTrain.common;

import ijij41.togetherTrain.Constants;
import ijij41.togetherTrain.R;
import ijij41.togetherTrain.participate.TrainListParticipateActivity;
import ijij41.togetherTrain.recruit.TrainListRecruitActivity;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import android.app.*;
import android.content.*;
import android.os.*;
import android.util.*;
import android.view.*;
import android.widget.*;

public abstract class AbstractSearchTrainActivity extends Activity implements
		Constants {

	private ArrayList<AbstractSearchTrainActivity.SearchRow> listSearch;

	private SearchAdapter sa;

	protected String search_date;
	protected String search_time;

	protected String search_startStation;

	protected String search_endStation;

	protected String search_person;

	
	protected void setLabelTitle(String title_text){
		TextView trainNumTv = ((TextView) findViewById(R.id.tv_title));
		trainNumTv.setText(title_text);
	}
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.search);

		initComponent();

	}

	private void initComponent() {
		initValue();

		findViewById(R.id.btn_back_img).setOnClickListener(mClickListener);
		findViewById(R.id.btn_search).setOnClickListener(mClickListener);

		listSearch = new ArrayList<AbstractSearchTrainActivity.SearchRow>();

		SearchRow sr = new SearchRow();
		sr.setLabel("출발시간");
		sr.setContent(search_date + " " + search_time);
		listSearch.add(sr);

		sr = new SearchRow();
		sr.setLabel("출발역");
		sr.setContent(search_startStation);
		listSearch.add(sr);

		sr = new SearchRow();
		sr.setLabel("도착역");
		sr.setContent(search_endStation);
		listSearch.add(sr);

		sr = new SearchRow();
		sr.setLabel("인원");
		sr.setContent(search_person);
		listSearch.add(sr);

		sa = new SearchAdapter(this, R.layout.search_row, listSearch);

		ListView list = (ListView) findViewById(R.id.list);
		list.setAdapter(sa);
		list.setOnItemClickListener(mItemClickListener);
	}

	private void initValue() {

		TimeZone tz = TimeZone.getTimeZone("GMT+09:00");
		Calendar c = Calendar.getInstance(tz);
		int mYear = c.get(Calendar.YEAR);
		int mMonth = c.get(Calendar.MONTH);
		int mDay = c.get(Calendar.DAY_OF_MONTH);
		int mHour = c.get(Calendar.HOUR_OF_DAY);
		int mMinute = c.get(Calendar.MINUTE);

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");

		SimpleDateFormat sdf_date = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat sdf_time = new SimpleDateFormat("HH:mm");

		Date date;
		try {
			date = sdf.parse(mYear + "-" + (mMonth + 1) + "-" + mDay + " "
					+ mHour + ":" + mMinute);

			search_date = sdf_date.format(date);
			search_time = sdf_time.format(date);

		} catch (ParseException e) {
			e.printStackTrace();
		}

		search_startStation = "서울";
		search_endStation = "서울";
		search_person = "1";

		// for TEST
		// search_date = "2012-04-28";
		search_date = "2007-03-03";
		search_time = "10:00";
		search_startStation = "서울";
		search_endStation = "부산";
		search_person = "1";

	}

	
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		Log.v("JH", "requestCode: " + requestCode + " , resultCode: "
				+ resultCode);

		// Explain : This mechanism
		// This mechanism can be used in case that an activity should get some
		// data from other activity
		// which was called to get some selection from User

		if (requestCode == TIME_CODE && resultCode == TIME_OK) {

			search_date = data.getExtras().getString("CAL_DATE");
			search_time = data.getExtras().getString("CAL_TIME");

			listSearch.get(0).setContent(search_date + " " + search_time);
			sa.notifyDataSetChanged();
		}

		if (requestCode == START_STATION_CODE && resultCode == STATION_OK) {
			search_startStation = data.getAction();
			listSearch.get(1).setContent(search_startStation);
			sa.notifyDataSetChanged();
		}

		if (requestCode == DEST_STATION_CODE && resultCode == STATION_OK) {
			search_endStation = data.getAction();
			listSearch.get(2).setContent(search_endStation);
			sa.notifyDataSetChanged();
		}

		if (requestCode == PERSON_CODE && resultCode == PERSON_OK) {
			search_person = data.getAction();
			listSearch.get(3).setContent(search_person);
			sa.notifyDataSetChanged();
		}

	}

	AdapterView.OnItemClickListener mItemClickListener = new AdapterView.OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int position,
				long arg3) {

			// String msg = items.get(arg2);
			// Toast.makeText(SearchActivity.this, msg,
			// Toast.LENGTH_SHORT).show();

			Intent stationSearchActivity = null;
			Log.v("JH", "Select Posigion : " + position);
			switch (position) {

			case 0:
				Intent timeActivity = new Intent(
						AbstractSearchTrainActivity.this, TimeActivity.class);
				// timeActivity.putExtra("PERSON",
				// listSearch.get(3).getContent());
				startActivityForResult(timeActivity, TIME_CODE);
				break;

			case 1:
				// startActivity(stationSearchActivity);
				stationSearchActivity = new Intent(
						AbstractSearchTrainActivity.this,
						StationSearchActivity.class);
				startActivityForResult(stationSearchActivity,
						START_STATION_CODE);
				break;

			case 2:
				stationSearchActivity = new Intent(
						AbstractSearchTrainActivity.this,
						StationSearchActivity.class);
				startActivityForResult(stationSearchActivity, DEST_STATION_CODE);
				break;
			case 3:
				Intent personActivity = new Intent(
						AbstractSearchTrainActivity.this, PersonActivity.class);
				personActivity.putExtra("PERSON", listSearch.get(3)
						.getContent());
				startActivityForResult(personActivity, PERSON_CODE);
				break;
			default:
				break;
			}

		}

	};

	private Button.OnClickListener mClickListener = new Button.OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {

			case R.id.btn_back_img:
				finish();
				break;

			case R.id.btn_search:
				search_action();
				break;

			default:
				break;
			}

		}
	};

	class SearchRow {
		private String label;
		private String content;

		public String getLabel() {
			return label;
		}

		public void setLabel(String label) {
			this.label = label;
		}

		public String getContent() {
			return content;
		}

		public void setContent(String content) {
			this.content = content;
		}

	}

	class SearchAdapter extends ArrayAdapter<SearchRow> {

		private ArrayList<SearchRow> items;

		public SearchAdapter(Context context, int textViewResourceId,
				ArrayList<SearchRow> objects) {
			super(context, textViewResourceId, objects);

			items = objects;

		}

		public View getView(int position, View v, ViewGroup parent) {
			// layout에다가 데이터를 담아서 리턴시켜야 하는데, layout을 어떻게 초기화 시키는가?
			// Inflater는 지정한 layout의 객체들을 모두 생성하고 생성된 객체간의 계층구조까지 완성해주는 놈
			LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(R.layout.search_row, null);

			// 보여주어야할 항목이 몇번째 항목이냐에 따라 그에 맞는 데이터 추츨
			SearchRow u = items.get(position);
			if (u != null) {
				TextView tv1 = (TextView) v.findViewById(R.id.tv1);
				TextView tv2 = (TextView) v.findViewById(R.id.tv2);
				if (tv1 != null)
					tv1.setText(u.getLabel());
				if (tv2 != null)
					tv2.setText(u.getContent());
			}
			return v;
		}

	}

	
	abstract protected void search_action();
	
//
//		Intent trainListActivity = new Intent(AbstractSearchTrainActivity.this,
//		// SearchTrainListParticipateActivity.class);
//				SearchTrainListRecruitActivity.class);
//
//		/*
//		 * trainListActivity.putExtra("time","10:00");
//		 * trainListActivity.putExtra("startStation","서울");
//		 * trainListActivity.putExtra("endStation","부산");
//		 * trainListActivity.putExtra("numPerson","1");
//		 */
//
//		trainListActivity.putExtra("date", search_date);
//		trainListActivity.putExtra("time", search_time);
//		trainListActivity.putExtra("startStation", search_startStation);
//		trainListActivity.putExtra("endStation", search_endStation);
//		trainListActivity.putExtra("numPerson", search_person);
//
//		startActivity(trainListActivity);
//
//	}
}
